﻿using UnityEngine;
using BilibiliUtilities.Live.Message;

namespace PnFramework.Unity
{
    public class DanmuInstruction
    {
        public const string Ctrl = "@Ctrl";
        public const string Shop = "@Shop";
        public const string Interactive = "@Interactive";

        public struct Info
        {
            public int uid;
            public string text;
            public string nickname;
        }
        private Info mInfo;

        public delegate bool DanmuFilter(ref string filted);
        /// <summary>
        /// 委托数组。。。DanmuFilter[]
        /// </summary>
        private DanmuFilter[] mFilters =
        {
            (ref string filted)=>{filted = filted.Trim(); return filted.Length > 1; },//去头尾空格
            (ref string filted)=>{filted = filted.Substring(1); return filted.Length > 0; }//过滤指令字符
        };
        public void OnTrigger(DanmuMessage danmu)
        {
            if (!mFilters[0](ref danmu.Content)) return;//去头尾空格
            mInfo.text = danmu.Content;
            if (!mFilters[1](ref mInfo.text)) return;//过滤指令字符
            mInfo.uid = (int)danmu.UserId;
            mInfo.nickname = danmu.Username;
            switch (danmu.Content[0])
            {
                case '$': EventMgr.Instance.Trigger(Ctrl, mInfo); break;
                case '#': EventMgr.Instance.Trigger(Shop, mInfo); break;
                case '@': EventMgr.Instance.Trigger(Interactive, mInfo); break;
                default: Debug.Log("指令标识错误"); break;
            }
        }
    }
}
